package cc.wsyw126.java.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * Created by junpeng.yang on 16/8/7.
 */

public class InvokeMethod {
    public <T> T retryTimes(Class<?> clazz,String methodName,Object[] paras) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        int n = 0;
        for (Object o: paras){
            n++;
        }

        Class[] parasClazz = new Class[n];

        int i =0;
        for (Object o: paras){
            parasClazz[i]=o.getClass();
            i++;
        }

        Method method = clazz.getMethod(methodName,parasClazz);
        System.out.println(method.getReturnType());
        return (T) method.invoke(clazz.newInstance(), paras);
    }

    public <T> void retry(Class<?> clazz,String methodName,Object paras) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        Method method = clazz.getMethod(methodName,paras.getClass());

        System.out.println(method.getReturnType());
        method.invoke(clazz.newInstance(), paras);
    }


    public <T> T retryTimesArrayList(Class<?> clazz,String methodName,Object[] paras) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        ArrayList<Class> classArrayList = new ArrayList<Class>();
        for (Object o: paras){
            classArrayList.add(o.getClass());
        }

        Class[] parasClazz = classArrayList.toArray(new Class[classArrayList.size()]);

        Method method = clazz.getMethod(methodName,parasClazz);
        System.out.println(method.getReturnType());
        return (T) method.invoke(clazz.newInstance(), paras);
    }
}
